package com.ynny.chapter14.contoller;

import com.ynny.chapter14.pojo.Student;
import com.ynny.chapter14.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentContoller {
    @Autowired
    private StudentService studentService;
    @PostMapping("/login")
    public String login(@RequestBody Student student) {
        System.out.println(student);
        try {
            List<Student> login = studentService.login(student);
            System.out.println("Login result: " + login);
            if (login != null && !login.isEmpty()) {
                return "ok";
            } else {
                return "no";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }


    @GetMapping("/findAll")
    public List<Student> findAll() {
        List<Student> all = studentService.findAll();
        return all;
    }

    @PostMapping("/add")
    public int add(@RequestBody Student student) {
        int i = studentService.add(student);
        return i;
    }

    @PutMapping("/update")
    public int update(@RequestBody Student student) {
        int i = studentService.update(student);
        return i;
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable(name = "id") Integer id) {
        int i = studentService.delete(id);
        return i;
    }

    @GetMapping("/selectone")
    public List<Student> selectone(@RequestBody Student student) {
        List<Student> one = studentService.findOne(student);
        return one;
    }
}
